package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.BankConfig;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.BankConfigService;
import co.yixiang.modules.system.dto.BankConfigDto;
import co.yixiang.modules.system.dto.queryCriteria.BankConfigQueryCriteria;
import co.yixiang.modules.system.mapper.BankConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author zf
* @date 2021-09-12
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "bankConfig")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BankConfigServiceImpl extends BaseServiceImpl<BankConfigMapper, BankConfig> implements BankConfigService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(BankConfigQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<BankConfig> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(BankConfigDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<BankConfig> queryAll(BankConfigQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(BankConfig.class, criteria));
    }


    @Override
    public void download(List<BankConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BankConfigDto bankConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", bankConfig.getUid());
            map.put("名称", bankConfig.getName());
            map.put("银行名称", bankConfig.getBank());
            map.put("银行卡号", bankConfig.getBankNumber());
            map.put("创建时间", bankConfig.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
